import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.SQLOutput;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Test {
    public static void main1(String[] args){
        checkClass(new Cat());
        checkClass(new Dog());
    }

    private static void checkClass(Animal animal){
        animal.eat();
        if(animal instanceof Cat){
            ((Cat) animal).catchMouse(); // 根据存储的类型来进行向下转型
        }else{
            ((Dog)animal).play();
        }
    }

    public static void main2(String[] args) {
        try {
            FileInputStream file = new FileInputStream("c:/image/img.png");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /*
    InputMismatchException
     */
    public static void main3(String[] args) {
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        System.out.println(num);
    }

    public static void main4(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入一个整数：");
        String str = in.next();

        int num = Integer.parseInt(str);
        System.out.println(num);
    }

    /*
    异常的处理
     */
    public static void main5(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入两个整数:");
        while(true){
            try {
                int a = in.nextInt();
                int b = in.nextInt();
                int ret = a / b;
                System.out.println("商是："+ ret);
                break;
            }catch (ArithmeticException e){
                e.printStackTrace();
                System.out.println("除数不能为0！");
           }catch (InputMismatchException e){
                e.printStackTrace();
                System.out.println("除数与被除数都必须为整数");
                in.nextLine();// 读取流中的非整数，不然会陷入死循环！
            }
            System.out.println("请重新输入：");
        }
        System.out.println("程序结束~");
    }

    /*
    finally 的作用展示
     */
    public static void main6(String[] args) {
        int[] arr = null;
        int index = MyArrays.indexOf(arr, 5);
        System.out.println("index = " + index);
    }

    /*
    throws 的使用
     */
    public static void main7(String[] args) {
        Scanner in = new Scanner(System.in);

        while(true){
            try {
                System.out.println("请指定要读取的文件：");
                String fileName = in.nextLine();
                readFile(fileName);
                break;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.out.println("文件不存在");
                System.out.println("请重新指定");
            }
        }

    }

    public static void readFile(String filePathName) throws FileNotFoundException{
        FileInputStream file = new FileInputStream(filePathName);
    }

    /*
    throw 的使用
     */
    public static void main8(String[] args) {
        Scanner in = new Scanner(System.in);
        while(true){
            try {
                System.out.println("请输入行数：");
                int row = in.nextInt();
                System.out.println("请输入列数：");
                int column = in.nextInt();
                System.out.println("请输入字符：");
                char ch = in.next().charAt(0);
                GraphicTools.printRectangle(row, column, ch);
                break;
            }catch (Exception e){
                System.out.println("请重新输入，原因是："+ e.getMessage());
            }
        }
    }

    /*
    自定义异常类型
     */
    public static void main9(String[] args) {
        Scanner in = new Scanner(System.in);
        while(true){
            try {
                System.out.println("请输入年龄：");
                int age = in.nextInt();
                if(age < 0 || age > 120){
                    throw new AgeIllegalException("年龄必须在0-120之间！");
                }
                System.out.println("年龄为："+age);
                break;
            }catch (Exception e){
                System.out.println("请重新输入，原因是："+e.getMessage());
            }

        }
    }

    /*
    Object 类中， getClass方法
     */
    public static void main(String[] args) {
        introduction(new Flower());
        introduction(new Tree());
    }

    public static void introduction(Botany botany){
        System.out.println("植物是："+botany.getClass());
        botany.introduction();
    }




}


